package com.lancer.backlog.controller;

import com.alibaba.fastjson.JSONObject;
import com.lancer.backlog.beans.UserInfo;
import com.lancer.backlog.common.bean.ResultBean;
import com.lancer.backlog.common.exception.ParamException;
import com.lancer.backlog.common.impl.ResponseResult;
import com.lancer.backlog.service.UserService;
import com.mysql.cj.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Auther: lcDai
 * @Date: 2020/8/17 14:08
 * @UpdateUser:
 * @UpdateDate:
 * @Version: 1.0
 **/

@RestController
@RequestMapping(value = "/user")
public class UserController {
    private final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    /**
     * 获取当前登录的用户信息
     *
     * @return
     */
    @RequestMapping("/getLoginUser")
    @ResponseResult
    public UserInfo getLoginUser() {


        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        // 没登录
        if (auth instanceof AnonymousAuthenticationToken) {
            return null;
        }else{
            // 已登录
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
            String userName = userDetails.getUsername();
            return userService.getUserByUsername(userName);
        }

    }



    /**
     * 获取用户列表
     *
     * @return
     */
    @RequestMapping("/getUserList")
    @ResponseResult
    public List<UserInfo> getUserList() {
        return userService.getUserList();
    }

    /**
     * 根据ID获取用户信息
     * @param id
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseResult
    public UserInfo getUserInfo(Integer id) {
        return userService.getUserInfo(id);
    }

    /**
     * 注册账号
     * @param userInfo
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseResult
    public void addUser(@RequestBody UserInfo userInfo){
        userService.addUser(userInfo);
    }

    /**
     * 修改用户信息
     * @param userInfo
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseResult
    public void updateUser(@RequestBody UserInfo userInfo){
        if(StringUtils.isNullOrEmpty(userInfo.getUsername() )){
            throw new ParamException("用户名不能为空");
        }
        userService.updateUser(userInfo);
    }

}
